\documentclass[12pt]{article}
\usepackage{times}
\usepackage{graphicx}
\usepackage{sidecap}
\usepackage{wrapfig}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\baselinestretch}{1.2}
\newenvironment{mylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}
\newenvironment{mylisting2}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}

%%% This file is Open Content

\begin{document}
	\textbf{{\Huge Castle SDL}}
{\center{Dec 29, 2011, 
	Johan Ceuppens, Theo D'Hondt - Vrije Universiteit Brussel}}
\\
{\large{\textbf{\center Abstract}}}
\\
\\
\section{\large Introduction}



\begin{mylisting}
\begin{verbatim}

	
#ifndef ENTITY_H
#define ENTITY_H

#include<SDL.h>
#include<string>
#include<list>
#include"Img.h"
#include"ImgSys.h"

namespace castlesdl
{
namespace main 
{
using namespace std
class Entity 
{
	public:
		Entity(int xx, int yy, int ww, int hh);
		virtual ~Entity() {}
		void moveLeft();
		void moveRight();
		void blit(SDL_Surface **screen);
		void addPicture(string filename);

	private:
		//NOTE
		Entity(const castlesdl::image::Img& rhs);
		Entity operator=(const castlesdl::image::Img& rhs);

		list<SDL_Surface *> _surfaces;
		int x,y;
	public:
};
}
}

#endif
#include<SDL.h>
#include<string>

namespace castlesdl
{
namespace image
{
using namespace std;
class Img
{
	public:
		Img(string filename);
		Img() : _surface(NULL) {}
		virtual ~Img() {}

	private:
		SDL_Surface *_surface;
	public:
		Img(const Img& rhs);
		Img operator=(const Img& rhs);
};
}
}

#ifndef IMGSYS_H
#define IMGSYS_H

#include<SDL.h>
#include<string>
#include<list>
#include<unistd.h>
namespace castlesdl
{
namespace image
{

using namespace std;

class ImgSys
{
	public:
		ImgSys() : _colorkey(0x000000) {}
		virtual ~ImgSys() {}

		int reload(string filename, SDL_Surface **surface);
		int load(string filename, SDL_Surface **surface);
		int load(string filename, list<SDL_Surface *> *surfaces);

	protected:

	private:
		int filein(string filename, SDL_Surface **surface = NULL);		
	private:
		uint32_t _colorkey;

};

}
}


#endif
#ifndef PLAYER_H
#define PLAYER_H

#include<SDL.h>
#include<string>
#include"Img.h"
#include"ImgSys.h"

namespace castlesdl
{
namespace main 
{
using namespace std;
class Player
{
	public:
		Player(int xx, int yy);
		virtual ~Player() {}
		void moveLeft();
		void moveRight();
		void blit(SDL_Surface **screen);

	private:
		//NOTE
		Player(const castlesdl::image::Img& rhs);
		Player operator=(const castlesdl::image::Img& rhs);

		SDL_Surface *_surface;
		int x,y;
	public:
};
}
}

#endif
#include<SDL.h>
#include<string>

namespace castlesdl
{
namespace image
{

/*
 * State System for images
 */

using namespace std;

template<class I>
class StateImg
{
	public:
		StateImg(); 
		virtual ~StateImg();

	private:
		I *_state;
	public:
		StateImg(const StateImg& rhs);
		StateImg operator=(const StateImg& rhs);
};
}
}

#include<string>

namespace castlesdl
{

const char *cs(std::string s);


}
#include"Entity.h"

namespace castlesdl
{
namespace main 
{

using namespace std;

Entity::Entity(int xx, int yy, int ww, int hh) : _surfaces() {

	x = xx;
	y = yy;

}

void Entity::moveLeft()
{
	x += -5;
}

void Entity::moveRight()
{
	x += 5;
}

void Entity::blit(SDL_Surface **screen)
{
	SDL_Rect rectangle;
	rectangle.x = x;
	rectangle.y = y;
	rectangle.w = 800;
	rectangle.h = 600;
	SDL_BlitSurface (_surface,&rectangle,*screen,NULL);
}

void Entity::addPicture(string filename)
{

	SDL_Surface *surface;
	castlesdl::image::ImgSys imgsys;
	imgsys.load(filenamel$.c_str(), _surfaces);

}

}
}
#include"Img.h"
#include"ImgSys.h"

namespace castlesdl
{
namespace image
{

using namespace std;

Img::Img(string filename) : _surface(NULL) {
	ImgSys imgsys;
	imgsys.load(filename, &_surface);
}

Img::Img(const Img& rhs)
{
	_surface = rhs._surface;

}

Img Img::operator=(const Img& rhs)
{

	if (&rhs == this) { return *this; }

	_surface = rhs._surface;

	return (*this);
}

}
}
#include<SDL.h>
#include<SDL_image.h>
#include"ImgSys.h"
#include "util.h"

namespace castlesdl
{
namespace image
{

using namespace std;

int ImgSys::load(string filename, SDL_Surface **surface)
{
	if ( !filename.length() ) { *surface = NULL; return(-1); } 
	if ( filein(filename, surface) && surface != NULL) { return(0); }
	//never reached.
	return -1;
}

int ImgSys::load(string filename, list<SDL_Surface *> *surfaces)
{
	SDL_Surface *surface = (SDL_Surface *)0;
	if ( !filename.length() ) { delete surface; return(-1); } 
	if ( filein(filename, &surface) < 0 || surface == NULL ) { return(-1); }

	surfaces->push_back(surface);
	list<SDL_Surface *>::iterator vi;
	for (vi = surfaces->begin(); vi != surfaces->end(); vi++)
		;

	--vi;
	memcpy ((*vi), surface, sizeof(surface));
			
	return 0;
}

int ImgSys::reload(string filename, SDL_Surface **surface)
{
	if ( !*surface ) { SDL_FreeSurface(*surface); *surface = NULL; }
	filein(filename, surface); 
	return(0);
}

int ImgSys::filein(string filename, SDL_Surface **surface)
{
	SDL_Surface *ts = IMG_Load(cs(filename));//cs returns c_str
	*surface = SDL_DisplayFormat(ts);
	SDL_SetColorKey (*surface,SDL_SRCCOLORKEY,_colorkey);
}

}
}
#include <SDL.h>
#include <iostream>
#include"ImgSys.h"
#include "Player.h"

using namespace std;

int main()
{


	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Surface *displaysurface = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE);
	SDL_Rect displayrectangle;
	displayrectangle.x = 0;
	displayrectangle.y = 0;
	displayrectangle.w = 800;
	displayrectangle.h = 600;


	castlesdl::image::ImgSys imgsys;

	SDL_Surface *opening = NULL;
	imgsys.load("./pics/opening.jpg", &opening);
	for (;;) 
	{


			SDL_Event e;
			if (SDL_WaitEvent(&e)/*FIXME SDL_PollEvent(&e)*/) 
			{
				int xx, yy;
				Uint8 mousestate = SDL_GetMouseState(&xx, &yy);

				if (e.button.type == SDL_MOUSEBUTTONDOWN) 
				{
					break;
				}
				if (e.button.type == SDL_MOUSEBUTTONUP) 
				{
					break;
				}
			switch (e.type) {
			case SDL_QUIT:{
				goto looplabel1;
				break;
			}
			case SDL_KEYDOWN:{
				switch (e.key.keysym.sym){
				case SDLK_ESCAPE:{
					goto looplabel1;
					break;
				}
				case SDLK_BACKSPACE:{
					cout<<"backspace pressed\n"<<endl;
					goto looplabel1;
					break;
				}
				case SDLK_RETURN:{
					cout<<"return pressed\n"<<endl;
					goto looplabel1;
					break;
				}
				default:
					cout<<"key pressed\n"<<endl;
					goto looplabel1;
					break;
				}
				}
			}
	}
	SDL_BlitSurface (opening,&displayrectangle,displaysurface,NULL);
	SDL_Flip(displaysurface);
	}

looplabel1:	

	castlesdl::main::Player player(0,200);

	for (;;) 
	{


			SDL_Event e;
			if (SDL_WaitEvent(&e)/*FIXME SDL_PollEvent(&e)*/) 
			{
				int xx, yy;
				Uint8 mousestate = SDL_GetMouseState(&xx, &yy);

				if (e.button.type == SDL_MOUSEBUTTONDOWN) 
				{
				}
				if (e.button.type == SDL_MOUSEBUTTONUP) 
				{
				}
			switch (e.type) {
			case SDL_QUIT:{
				exit(0);
				break;
			}
			case SDL_KEYDOWN:{
				switch (e.key.keysym.sym){
				case SDLK_ESCAPE:{
					exit(0);
					break;
				}
				case SDLK_BACKSPACE:{
					cout<<"backspace pressed\n"<<endl;
					break;
				}
				case SDLK_RETURN:{
					cout<<"return pressed\n"<<endl;
					break;
				}
				case SDLK_LEFT:{
					cout<<"left pressed\n"<<endl;
					player.moveLeft();
					break;
				}
				case SDLK_RIGHT:{
					cout<<"right pressed\n"<<endl;
					player.moveRight();
					break;
				}
				default:
					cout<<"key pressed\n"<<endl;
					break;
				}
				}
			}
	}
	
	player.blit(&displaysurface);
	SDL_Flip(displaysurface);
	}	
}
#include"Player.h"

namespace castlesdl
{
namespace main 
{

using namespace std;

Player::Player(int xx, int yy) : _surface(NULL) {
	castlesdl::image::ImgSys imgsys;
	imgsys.load("./pics/player1.png", &_surface);

	x = xx;
	y = yy;

}

void Player::moveLeft()
{
	x += -5;
}

void Player::moveRight()
{
	x += 5;
}

void Player::blit(SDL_Surface **screen)
{
	SDL_Rect rectangle;
	rectangle.x = x;
	rectangle.y = y;
	rectangle.w = 800;
	rectangle.h = 600;
	SDL_BlitSurface (_surface,&rectangle,*screen,NULL);
}

}
}
#include"StateImg.h"
#include"ImgSys.h"

namespace castlesdl
{
namespace image
{

using namespace std;

template<class C>
StateImg<C>::StateImg() : _state(NULL) 
{

}

template<class C>
StateImg<C>::StateImg(const StateImg& rhs)
{
	_state = rhs._state;

}

template<class C>
StateImg<C> StateImg<C>::operator=(const StateImg& rhs)
{

	if (&rhs == this) { return *this; }

	_state = rhs._state;

	return (*this);
}

}
}
#include"util.h"

namespace castlesdl
{
	
const char *cs(std::string s)
{
	return s.c_str();
}

}
CXX=g++
CFLAGS=-O -I/usr/include/SDL -L/usr/lib/ -lSDL -lSDL_image

OBJS=main.o\
     Img.o\
     StateImg.o\
     ImgSys.o\
     Player.o\
     util.o\

all: castlesdl

.cc.o:
	${CXX} ${CFLAGS} -c $<

castlesdl: ${OBJS}
	${CXX} -o castlesdl *.o ${CFLAGS}
clean:
	rm *.o *~ castlesdl

\end{verbatim}
\end{mylisting}


\section{\large Conclusion}


\bibliographystyle{plain}
\bibliography{refs} % refs.bib

\end{document}


